const {
    async
} = require("../../utils/runtime")
var app =getApp()
var tableName = 'userAddress'
var keyId = ''
var userPhone = app.globalData.phone
// pages/addresDetail/addresDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selected: false,
        userAddress: {}
    },
    getUserAllAddress: function () {
        return new Promise((resolve, reject) => {
            let Product = new wx.BaaS.TableObject(tableName)
            let query = new wx.BaaS.Query()
            query.compare('userPhone', '=', app.globalData.phone)
            Product.setQuery(query).find().then(res => {
                // success
                console.log(res)
                resolve(res)
            }, err => {
                // err
                console.log('没有找到')
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        console.log(options.id)
        this.setData({
            index:options.id
        })
        var resData = await this.getUserAllAddress()
        if (resData.data.objects[0].userAllAddress != null) {
            this.data.addressArr = resData.data.objects[0].userAllAddress
            console.log(this.data.addressArr)
            this.data.userAddress = this.splitArr(this.data.addressArr)
            console.log('收获地址', this.data.userAddress)
            keyId = resData.data.objects[0].id
            
        }
        this.setData({
            userAddress: this.data.userAddress
        })
        // app.globalData.zhuzhiaddress = 
    },
    splitArr: function (arr) {
        var arrSplit = []
        var tempArr = []
        var j = 0
        for (var i = 0; i < arr.length; i++) {
            tempArr.push(arr[i])
            j = j + 1
            if (j == 5) {
                arrSplit.push(tempArr)
                tempArr = []
                j = 0
            }
        }
        return arrSplit
    },
    Address:function(e){
        for(var i = 0 ; i< this.data.userAddress.length ;i++){
            console.log(this.data.userAddress[i])
            if(this.data.userAddress[i][4] == 1){
                app.globalData.zhuzhiaddress = this.data.userAddress[i][3] + ' '+ this.data.userAddress[i][1]
                console.log(this.data.userAddress[i])
            }
        }
        wx.navigateBack({
        //   delta: 0,
        })
    },
    deleteAddress: function (e) {
        var that = this
        wx.showModal({
            title: '提示',
            content: '确认删除',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    var tempIndex = e.target.dataset.id
                    that.data.userAddress.splice(tempIndex, 1)
                    console.log(that.data.userAddress.join(''))
                    that.setData({
                        userAddress: that.data.userAddress
                    })
                    var conactArr = that.getNewArr()
                    console.log(conactArr)
                    that.updateUserAddress(conactArr)
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
        console.log(e)
    },
    getNewArr: function (e) {
        var conactArr = []
        for (var i = 0; i < this.data.userAddress.length; i++) {
            conactArr = conactArr.concat(this.data.userAddress[i])
        }
        return conactArr
    },
    updateUserAddress: function (e) {
        let tableName = 'userAddress'
        let recordID = keyId // 数据行 id

        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)

        product.set('userAllAddress', e)
        product.update().then(res => {
            // success
            console.log('更新成功')
        }, err => {
            // err
        })
    },
    setZero: function () {
        for (var i = 0; i < this.data.userAddress.length; i++) {
            this.data.userAddress[i][4] = '0'
        }
    },
    setOne: function (e) {
        this.data.userAddress[e][4] = '1'
        app.globalData.address = this.data.userAddress[e][3] + ' ' + this.data.userAddress[e][1]
        console.log(app.globalData.address)
    },
    setSelected: function (e) {
        var tempIndex = e.currentTarget.dataset.testid
        console.log(tempIndex)
        this.setZero()
        this.setOne(tempIndex)
        var conactArr = this.getNewArr()
        this.updateUserAddress(conactArr)
        this.setData({
            userAddress: this.data.userAddress
        })
    },
    changeInfo: function (e) {
        wx.navigateTo({
            url: "/pages/updateAddress/updateAddress?userAddress=" + this.data.userAddress + '&id=' + e.currentTarget.dataset.id + '&keyid=' + keyId,
        })
    },
    addAddress: function () {
        if(this.data.index == 1 || this.data.index){
            wx.navigateTo({
                url: "/pages/addAddress/addAddress?id="+this.data.index,
            })
        }else{
            wx.navigateTo({
                url: "/pages/addAddress/addAddress?id",
            })
        }
       
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})